<?php


namespace app\common\service\companyIndustry\realtime;

use app\common\service\BaseService;
use app\common\service\ControlService;
use app\common\service\MongoService;

/**
 * 液气比（脱硫）
 */
class SulfurLiquidGasRatioService extends BaseService
{
    // +----------------------------------------------------------------------
    // | 字段的计算-液气比，单位 L/m3
    // +----------------------------------------------------------------------
    /**
     * 查询液气比的总和
     * 单位是：立方米/小时
     * @param $facilityId
     * @param $planNumber
     * @return array|float|int
     * @throws \MongoDB\Driver\Exception\Exception
     * @author hongwei 2019-07-22
     */
    public function getSulfurLiquidGasFieldOfSumEverDay($facilityId, $planNumber)
    {
        $mongo = new MongoService();
        $control = new ControlService();
        //参数
        $field = '$liquid_gas_ratio';
        //减排前参数
        $startDay      = $control->getBeforeStartTime($planNumber);
        $endDay        = $control->getBeforeEndTime($planNumber);
        //得到值
        $data = $mongo->getSulfurFieldOfSumOfFacility($startDay, $endDay, $facilityId, $field, true);
        foreach ($data as $k => $v) {
            $everyStartDay = date('Y-m-d', strtotime($v['_id']->publish_time)).' 00:00:00';
            $everyEndDay = date('Y-m-d', strtotime($v['_id']->publish_time)).' 23:59:59';
            $workingMinutesFacility =  $mongo->workingMinutesFacility($everyStartDay, $everyEndDay, $facilityId);
            $data[$k]['facility_id']  = $v['_id']->facility_id;
            $data[$k]['publish_time'] = $v['_id']->publish_time;
            $data[$k]['publish_day']  = date('m/d', strtotime($v['_id']->publish_time));
            $data[$k]['liquid_data']  = $v['field'] / $workingMinutesFacility;
            unset($data[$k]['_id']);
            unset($data[$k]['field']);
        }
        return $data;
    }

    /**
     * （减排前）脱硫液气比趋势图 - 返回天数
     * @param int $facilityId
     * @param string $plan 方案值（这个值是吕凯加的）
     * @param string $planNumber
     * @param string $plan
     * @return float|int
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function singleCompanySingleDrainSingleFacilityDetails($plan, $planNumber, $facilityId)
    {
        $liquidData = $this->getSulfurLiquidGasFieldOfSumEverDay($facilityId, $planNumber);
        $date = [];
        $data = [];
        foreach ($liquidData as $k => $v) {
            array_push($date, $v['publish_day']);
            array_push($data, round($v['liquid_data'], 3));
        }
        array_multisort($date, $data);
        $result = [
            'date' => $date,
            'data' => [$plan['data'][0],$data],
        ];
        return $result;
    }

}